package com.hlm.demo.service.business;

import com.hlm.demo.entity.Account;
import com.hlm.demo.entity.BusinessInfo;
import com.hlm.demo.entity.CostInfo;
import com.hlm.demo.mapper.BusinessMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class BusinessService implements IBusinessService {

    @Autowired
    private BusinessMapper businessMapper;


    @Override
    public List<BusinessInfo> businessCheck(BusinessInfo businessInfo, String idCardNo, String order, Integer page, Integer count) {
        Map<String, Object> serviceMap = new HashMap<>();
        // TODO 参数判断
        page = (page - 1) * count;


        serviceMap.put("order", order);
        serviceMap.put("page", page);
        serviceMap.put("count", count);

        String unixHost = businessInfo.getUnixHost();
        String osUsername = businessInfo.getOsUsername();
        Integer serviceStatus = businessInfo.getServiceStatus();

        if (osUsername != null && !osUsername.trim().equals("")) {
            serviceMap.put("osUsername", osUsername);
        }
        if (unixHost != null && !unixHost.trim().equals("")) {
            serviceMap.put("unixHost", unixHost);
        }
        if (idCardNo != null && !idCardNo.trim().equals("")) {
            serviceMap.put("idCardNo", idCardNo);
        }


        serviceMap.put("serviceStatus", serviceStatus);

        return businessMapper.businessChecker(serviceMap);
    }

    @Override
    public Integer businessTotalCountCheck(BusinessInfo businessInfo, String idCardNo) {

        Map<String, Object> serviceMap = new HashMap<>();

        String unixHost = businessInfo.getUnixHost();
        String osUsername = businessInfo.getOsUsername();
        Integer serviceStatus = businessInfo.getServiceStatus();

        if (osUsername != null && !osUsername.trim().equals("")) {
            serviceMap.put("osUsername", osUsername);
        }
        if (unixHost != null && !unixHost.trim().equals("")) {
            serviceMap.put("unixHost", unixHost);
        }
        if (idCardNo != null && !idCardNo.trim().equals("")) {
            serviceMap.put("idCardNo", idCardNo);
        }


        serviceMap.put("serviceStatus", serviceStatus);

        return businessMapper.businessTotalCountChecker(serviceMap);
    }



    /**
     * 资费启用暂停删除
     *
     * @param serviceId
     * @param serviceStatus
     */
    @Override
    public void serviceSwitch(String serviceId, Integer serviceStatus) {

        Map<String, Object> serviceMap = new HashMap<>();
        // TODO 判断
        serviceMap.put("serviceId", serviceId);
        serviceMap.put("serviceStatus", serviceStatus);

        businessMapper.businessSwitcher(serviceMap);
    }

    /**
     * 根据身份证查询账务账号
     * @param idcardNo
     * @return
     */
    @Override
    public Account findAccountIdByIdcardNo(String idcardNo) {
        // TODO 判断身份证号
        return businessMapper.findAccountsIdByIdcardNo(idcardNo);

    }


    /**
     * 查询所有资费信息
     * @return
     */
    @Override
    public List<CostInfo> costCheck() {
        return businessMapper.costChecker();
    }


    /**
     * 添加业务
     * @param businessInfo
     */
    @Override
    public void addBusiness(BusinessInfo businessInfo) {

        businessMapper.businessAdder(businessInfo);
        System.out.println("业务 id: " + businessInfo.getServiceId());


    }

    /**
     * 更改业务
     * @param businessInfo
     */
    @Override
    public void modifyBusiness(BusinessInfo businessInfo) {

        businessMapper.businessModifier(businessInfo);

    }

    /**
     * 暂停时间记录器
     */
    @Override
    public void pauseTime(String serviceId) {

        businessMapper.pauseTimer(serviceId);

    }

    /**
     * 暂停时间记录取消器
     */
    @Override
    public void pauseTimeCancel(String serviceId) {

        businessMapper.pauseTimeCanceller(serviceId);

    }

    /**
     * 删除时间记录器
     */
    @Override
    public void deleteTime(String serviceId) {

        businessMapper.deleteTimer(serviceId);

    }


}
